package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.entity.ManagerUser;
import com.itany.exception.ManagerUserExistsException;
import com.itany.exception.ManagerUserIdNotException;
import com.itany.exception.ManagerUserNotExistsException;
import com.itany.service.ManagerUserService;
import com.itany.vo.ResponseResult;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Author：汤小洋
 * Date：2019-12-19 11:01
 * Description：<描述>
 */
@RestController

@RequestMapping("/managers")
public class ManagerUserController {

    @Autowired
    private ManagerUserService managerUserService;

    @GetMapping("/login")
    public ResponseResult login(ManagerUser user, HttpSession session){
        try {
            ManagerUser u = managerUserService.login(user);
            session.setAttribute("id",u.getId());
            System.out.println("--------------");
            System.out.println(session.getAttribute("id"));
            return ResponseResult.success(user.getId());
        } catch (ManagerUserNotExistsException e) {
            return ResponseResult.fail(e.getMessage());
        }
    }
    @GetMapping("/findByPage")
    public Map findByPage(Integer page, Integer rows){
        if (rows == null || rows<1){
            rows=1;
        }
        PageInfo<ManagerUser> pageInfo = managerUserService.findByPage(page,rows);
        Map map = new HashMap();
        map.put("total", pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        System.out.println(pageInfo.getList());
        return map;
    }

    @PostMapping("/addUser")
    public ResponseResult addUser(String username_add, String nickname_add){
        ManagerUser managerUser = new ManagerUser();
        managerUser.setUsername(username_add);
        managerUser.setPassword("123");
        managerUser.setNickname(nickname_add);
        try {
            managerUserService.addUser(managerUser);
            return ResponseResult.success(managerUser);
        } catch (ManagerUserExistsException e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }
    }

    @RequestMapping("/modifyUser")
    public ResponseResult modifyUser(Integer id, String username_modify, String nickname_modify){

        System.out.println("username_modify"+username_modify);
        System.out.println("nickname_modify"+nickname_modify);
        System.out.println("----------------");
        ManagerUser user = new ManagerUser();
        user.setId(id);
        user.setUsername(username_modify);
        user.setNickname(nickname_modify);
        try {
            managerUserService.modifyUser(user);
            return ResponseResult.success(user);
        } catch (ManagerUserExistsException e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }
    }
    @RequestMapping("/deleteUser")
    public ResponseResult deleteUser(Integer id){
        managerUserService.deleteUser(id);
        return ResponseResult.success();
    }

    @RequestMapping("/findById")
    public ResponseResult findByUserId(Integer id){
        try {
            ManagerUser user = managerUserService.findByUserId(id);
            return ResponseResult.success(user);
        } catch (ManagerUserIdNotException e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }
    }

    @RequestMapping("/modifyPassword")
    public ResponseResult findByUserId(Integer id,String password){
        if (password == null || password==""){
            password="123456";
        }
        ManagerUser user = new ManagerUser();
        user.setId(id);
        user.setPassword(password);
        managerUserService.modifyPassword(user);
        return ResponseResult.success(user);
    }
}
